<?php

namespace App\Admin\Actions\Good;

use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class SetPrice extends RowAction
{
    public $name = '定价';

    public function form()
    {
        // 文本输入框
        $this->text('price-' . $this->getRow()->id, '成本价')
            ->default($this->getRow()->price)
            ->rules('required|numeric|between:0.00,99.99', [
                'required' => '请输入价格',
                'numeric' => '请输入0-99.99之间的数字',
                'between' => '请输入0-99.99之间的数字'
            ]);
    }

    public function handle(Model $model, Request $request)
    {
        $model->fill(['price' => $request->get('price-' . $model->{$model->getKeyName()})]);

        if ($model->save() !== false) {
            return $this->response()->success('操作成功')->refresh();
        }
        return $this->response()->error('操作失败')->refresh();
    }

}
